#!/bin/sh
# PCP QA Test No. 1255
# Test various programs using pmLogOpen() with logfile == "-" to use stderr
#
# Copyright (c) 2018 Red Hat.  All Rights Reserved.
#
seq=`basename $0`
echo "QA output created by $seq"

# get standard environment, filters and checks
. ./common.product
. ./common.filter
. ./common.check

status=1	# failure is the default!
$sudo rm -rf $tmp $tmp.* $seq.full

_cleanup()
{
    cd $here
    $sudo rm -rf $tmp $tmp.*
}

trap "_cleanup; exit \$status" 0 1 2 3 15

_filter_dbpmda()
{
    sed \
        -e '/pmResult/s/ .* numpmid/ ... numpmid/' \
        -e "s;$PCP_PMDAS_DIR;\$PCP_PMDAS_DIR;" \
        -e "s/\.$DSO_SUFFIX$/.DSO_SUFFIX/g" \
        -e "s/\.$DSO_SUFFIX\([^a-zA-Z]\)/.DSO_SUFFIX\1/g" \
    # end
}

_filter()
{
    tee $seq.full \
    | sed -e 's/started.*$/started DATE/' | _filter_pmlogger_log | _filter_pmie_log \
    | _filter_pmcd_log | _filter_dbpmda | sed -e 's/'`hostname`'/HOST/g'
}

echo;echo == check log output to \"-\" '(stderr)'
src/chkopenlog 2 "-" 2>&1 | _filter

echo;echo == check pmlogger -l-
echo 'log mandatory on once { hinv.ncpu }' | pmlogger -l- -t 1s -s 2s $tmp.archive 2>&1 | _filter

echo;echo == check pmie -l-
echo hinv.ncpu | pmie -f -l- -T1s 2>&1 | _filter

echo;echo == check simple pmda logging to stderr instead of simple.log
$sudo TERM=ansi dbpmda -n $PCP_PMDAS_DIR/simple/root -ie <<End-of-File 2>$tmp.err | _filter
open pipe $PCP_PMDAS_DIR/simple/pmdasimple -d 253 -l-
close
End-of-File

# wait for pmdasimple to exit ...
#
rm -f $tmp.ok
for i in 1 2 3 4 5
do
    if $PCP_PS_PROG $PCP_PS_ALL_FLAGS | grep '[p]mdasimple .* -l-' >>$here/$seq.full
    then
	sleep 1
    else
	touch $tmp.ok
	break
    fi
done
[ -f $tmp.ok ] || echo "Arrgh ... pmdasimple did not exit, see $seq.full"
_filter <$tmp.err

# success, all done
status=0
exit
